package cn.lina.middleware.dynamic.thread.pool.sdk.domain;

import cn.lina.middleware.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author 24449
 * @date 2025/4/16
 * @description 动态线程池服务的实现类
 */
public class DynamicThreadPoolService implements IDynamicThreadPoolService{

    // 开启日志
    private final Logger logger = LoggerFactory.getLogger(DynamicThreadPoolService.class);

    // 应用名字
    private final String applicationName;
    // 线程池信息
    private final Map<String, ThreadPoolExecutor> threadPoolExecutorMap;
    // 构造函数，传递上面两个信息
    public DynamicThreadPoolService(String applicationName, Map<String, ThreadPoolExecutor> threadPoolExecutorMap) {
        this.applicationName = applicationName;
        this.threadPoolExecutorMap = threadPoolExecutorMap;
    }

    /**
     * 查询线程池列表
     * @return
     */
    @Override
    public List<ThreadPoolConfigEntity> queryThreadPoolList() {
        // keySet() 返回映射中所有 key 组成的 Set 视图
        Set<String> threadPoolBeanNames = threadPoolExecutorMap.keySet();
        ArrayList<ThreadPoolConfigEntity> threadPoolVos = new ArrayList<>();
        for (String threadPoolBeanName : threadPoolBeanNames) {
            // 根据名字从threadPoolExecutorMap中获取到对应的信息
            ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolBeanName);
            ThreadPoolConfigEntity entity = new ThreadPoolConfigEntity(applicationName, threadPoolBeanName);;
            entity.setCorePoolSize(threadPoolExecutor.getCorePoolSize());
            entity.setMaximumPoolSize(threadPoolExecutor.getMaximumPoolSize());
            entity.setPoolSize(threadPoolExecutor.getPoolSize());
            entity.setActiveCount(threadPoolExecutor.getActiveCount());
            entity.setQueueType(threadPoolExecutor.getQueue().getClass().getSimpleName());
            entity.setQueueSize(threadPoolExecutor.getQueue().size());
            entity.setRemainingCapacity(threadPoolExecutor.getQueue().remainingCapacity());
            threadPoolVos.add(entity);
        }
        return threadPoolVos;
    }

    /**
     * 通过线程池的名字查询该线程池的配置
     * @param threadPoolName
     * @return
     */
    @Override
    public ThreadPoolConfigEntity queryThreadPoolConfigByName(String threadPoolName) {
        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolName);
        // 防止出现空指针异常
        if(threadPoolExecutor == null)
            return new ThreadPoolConfigEntity(applicationName, threadPoolName);

        ThreadPoolConfigEntity entity = new ThreadPoolConfigEntity();
        entity.setCorePoolSize(threadPoolExecutor.getCorePoolSize());
        entity.setMaximumPoolSize(threadPoolExecutor.getMaximumPoolSize());
        entity.setPoolSize(threadPoolExecutor.getPoolSize());
        entity.setActiveCount(threadPoolExecutor.getActiveCount());
        entity.setQueueType(threadPoolExecutor.getQueue().getClass().getSimpleName());
        entity.setQueueSize(threadPoolExecutor.getQueue().size());
        entity.setRemainingCapacity(threadPoolExecutor.getQueue().remainingCapacity());
        return entity;
    }

    /**
     * 修改线程池的配置
     * @param threadPoolConfigEntity
     */
    @Override
    public void updateThreadPoolConfig(ThreadPoolConfigEntity threadPoolConfigEntity) {
        // 如果传入的对象为空，或者不是自己的应用，直接返回
        if(threadPoolConfigEntity == null || !applicationName.equals(threadPoolConfigEntity.getAppName()))
            return;

        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolConfigEntity.getThreadPoolName());
        // 空指针直接返回
        if(threadPoolExecutor == null) return;

        // 设置参数 调整核心线程数和最大线程数
        threadPoolExecutor.setCorePoolSize(threadPoolConfigEntity.getCorePoolSize());
        threadPoolExecutor.setMaximumPoolSize(threadPoolConfigEntity.getMaximumPoolSize());
    }
}
